package com.jian.sys.controller;


import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jian.sys.common.Constant;
import com.jian.sys.common.DataGridView;
import com.jian.sys.common.ResultObj;
import com.jian.sys.common.TreeNode;
import com.jian.sys.pojo.Dept;
import com.jian.sys.pojo.Notice;
import com.jian.sys.service.DeptService;
import com.jian.sys.vo.DeptVo;
import com.jian.sys.vo.NoticeVo;
import org.apache.commons.lang3.StringUtils;
import org.apache.tomcat.util.bcel.Const;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.expression.spel.ast.BooleanLiteral;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 剑
 * @since 2020-01-10
 */
@RestController
@RequestMapping("/dept")
public class DeptController {
    @Autowired
    DeptService deptService;

    @RequestMapping("/loadDeptMenuDtreeJson")
    public DataGridView loadDeptTitle(){
        List<Dept> list = deptService.list();
        List<TreeNode> treeNodes = new ArrayList<>();
        for (Dept dept : list) {
            Integer id = dept.getId();
            Integer pid = dept.getPid();
            String title = dept.getTitle();
            Boolean spread=dept.getOpen()== Constant.DEPT_TRUE?Constant.MENU_SPREAD_TRUE:Constant.MENU_SPREAD_FALSE;
            treeNodes.add(new TreeNode(id,pid,title,spread));
        }
        return new DataGridView(treeNodes);
    }
    /**
     * 查询所有和模糊查询
     */
    @RequestMapping("/loadAllDept")
    public DataGridView loadAllDept(DeptVo deptVo){
        IPage<Dept> page = new Page<>(deptVo.getPage(),deptVo.getLimit());
        QueryWrapper<Dept> wrapper = new QueryWrapper<>();
        wrapper.like(StringUtils.isNotBlank(deptVo.getTitle()),"title",deptVo.getTitle());
        wrapper.like(StringUtils.isNotBlank(deptVo.getRemark()),"remark",deptVo.getRemark());
        wrapper.like(StringUtils.isNotBlank(deptVo.getAddress()),"address",deptVo.getAddress());
        wrapper.orderByAsc("ordernum");
        wrapper.eq(deptVo.getId()!=null,"id",deptVo.getId()).or().eq(deptVo.getId()!=null,"pid",deptVo.getId());
        this.deptService.page(page,wrapper);
        return new DataGridView(page.getTotal(),page.getRecords());
    }

    /**
     * 获取最大的排序值
     * @return
     */
    @RequestMapping("/getMaxOrderNum")
    public Map<String,Object> getMaxOrderNum(){
        Map<String,Object> maxOrderNum =new HashMap<>();
        QueryWrapper<Dept> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("ordernum");
        List<Dept> list = this.deptService.list(wrapper);
        if(list.size()>0) {
            maxOrderNum.put("value", list.get(0).getOrdernum() + 1);
        }
        else {
            maxOrderNum.put("value",1);
        }
        return maxOrderNum;
    }




    /**
     * 添加
     * @param deptVo
     * @return
     */
    @RequestMapping("/addDept")
    public ResultObj addDept(DeptVo deptVo){
        try {
            deptVo.setCreatetime(new Date());
            this.deptService.save(deptVo);
            return ResultObj.ADD_SUCCESS;
        }catch (Exception e){
            e.printStackTrace();
            return ResultObj.ADD_ERROR;
        }
    }

    /**
     * 修改
     * @param deptVo
     * @return
     */
    @RequestMapping("/updateDept")
    public ResultObj updateDept(DeptVo deptVo){
        try {
            this.deptService.updateById(deptVo);
            return ResultObj.UPDATE_SUCCESS;
        }catch (Exception e){
            e.printStackTrace();
            return ResultObj.UPDATE_ERROR;
        }
    }
    /**
     * 检查删除的是否有子节点
     */
    @RequestMapping("/checkDeleteDeptChildren")
    public Map<String,Object>checkDeleteDeptChildren(DeptVo deptVo){
        Map<String,Object> map=new HashMap<>();
        QueryWrapper<Dept> wrapper = new QueryWrapper<>();
        wrapper.eq(deptVo.getId()!=null,"pid",deptVo.getId());
        List<Dept> list = this.deptService.list(wrapper);
        if (list.size()>0){
            map.put("value",true);
        }else{
            map.put("value",false);
        }
        return map;
    }


    /**
     * 删除
     * @param id
     * @return
     */
    @RequestMapping("/deleteDept")
    public ResultObj deleteDept(Integer id){
        try{
            this.deptService.removeById(id);
            return ResultObj.DELETE_SUCCESS;
        }catch (Exception e){
            e.printStackTrace();
            return ResultObj.DELETE_ERROR;
        }
    }
}

